<?php
/**
 * Class for limitting query and creating links for paging...
 * (Last modified 16-June-2011)
 *
 * @version 1.0
 * @author lucky <bogeyman2007@gmail.com>
 * @filesource paging.php
 * @license http://www.gnu.org/licenses/gpl.html
 */

/**
 * Testing.............
 */

/**
 * Class nicePaging
 */
class nicePaging{
    /**
     * @var connection Storing the database connection link
     * @access private
     */
    private $conn;
    
    /**
     * @var integer Storing the current page
     * @access private
     */
    private $page;
    
    /**
     * @var integer Storing the total pages
     * @access private
     */
    private $totalPages;
    
    /**
     * Constructor
     *
     * @access public
     * @param connection The database connection link (default=NULL)
     */
    public function __construct($conn=null){
	$this->conn=$conn;
    }
    
    
    /**
     * Method for limitting query result based on the requested page and rows per page 
     *
     * @access public
     * @param string $sql The SQL string (without LIMIT)
     * @param integer $page The page number that want to be displayed
     * @param integer $rowsPerPage Displayed rows per page
     * @return resultset Resultset from query
     */
    public function pagerQuery($sql, $page, $rowsPerPage){
	if($this->conn==null)
	    $result=mysql_query($sql);
	else
	    $result=mysql_query($sql, $this->conn);
	
	$totalRows=mysql_num_rows($result);
	$this->totalPages=intval($totalRows/$rowsPerPage) + ($totalRows%$rowsPerPage==0 ? 0 : 1);
	if($this->totalPages<1){
	    $this->totalPages=1;
	}
	
	$this->page=intval($page);
	if($this->page<1){
	    $this->page=1;
	}
	if($this->page>$this->totalPages){
	    $this->page=$this->totalPages;
	}
	
	$this->page-=1;
	if($this->page<0){
	    $this->page=0;
	}
	
	$result=mysql_query($sql." LIMIT ".$this->page*$rowsPerPage.", ".$rowsPerPage);
	$this->page+=1;
	
	return $result;
    }
    
    /**
     * Method for creating the links for paging
     *
     * @access public
     * @param string $link The page name
     * @param integer $conjunction Conjunction between page name and query string for page number
     * @param integer $maxPages Number of paging links that will be displayed
     * @return string Links for paging
     */
    public function createPaging($link, $conjunction="?", $maxPages=10){
        $start=((($this->page%$maxPages==0) ? ($this->page/$maxPages) : intval($this->page/$maxPages)+1)-1)*$maxPages+1;
        $end=((($start+$maxPages-1)<=$this->totalPages) ? ($start+$maxPages-1) : $this->totalPages);
        
        $paging='<ul class="nice_paging">';
        if($this->page>1){
            $paging.='<li><a href="'.$link.$conjunction.'page=1" title="First page">&lt;&lt;</a></li>';
            $paging.='<li><a href="'.$link.$conjunction.'page='.($this->page-1).'" title="Previous page">&lt;</a></li>';
        }
        
        if($start>$maxPages){
            $paging.='<li><a href="'.$link.$conjunction.'page='.($start-1).'" title="Page '.($start-1).'">...</a></li>';
        }
        
        for($i=$start;$i<=$end;$i++){
            if($this->page==$i){
                $paging.='<li class="current">'.$i.'</li>';
            }
            else{
                $paging.='<li><a href="'.$link.$conjunction.'page='.$i.'" title="Page '.$i.'">'.$i.'</a></li>';
            }
        }
        
        if($end<$this->totalPages){
            $paging.='<li><a href="'.$link.$conjunction.'page='.($end+1).'" title="Page '.($end+1).'">...</a></li>';
        }
        
        if($this->page<$this->totalPages){
            $paging.='<li><a href="'.$link.$conjunction.'page='.($this->page+1).'" title="Next page">&gt;</a></li>';
            $paging.='<li><a href="'.$link.$conjunction.'page='.$this->totalPages.'" title="Last page">&gt;&gt;</a></li>';
        }
        
        return $paging;
    }
}

/***** Test *****/